// components/price/price.js
const app = getApp()
import service from '../../utils/request'
import { formatTime, formatDate } from '../../utils/util'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    site: String,
    showPriceList: Boolean,
    
  },
  attached (){
    console.log('执行组件')
    this.handleTap()
  },

  /**
   * 组件的初始数据
   */
  data: {
    rateList: [],
    localRate: [],
    type:  1,
    userTime: [],
    groupPrice: [],
    currentIndex: 0,
    localIndex: 0,
  },
  
  /**
   * 组件的方法列表
   */
  methods: {
    
    //选择集团费率
    onClickRate(e){
      console.log(e)
      let type = e.target.dataset.type ? e.target.dataset.type : e.currentTarget.dataset.type
      let currRate = type== "1" ? this.data.localRate : this.data.groupPrice
      this.setData({
        rateList: currRate,
        type: type
      })
      console.log('type',this.data.type)
    },
      //费率列表
    handleTap(){
      // this.setData({
      //   showPriceList: true,
      //   currenSite: e.currentTarget.dataset.id
      // })
      let that = this
      let siteId = this.data.site
      let thisTime =new Date().getTime()
      let dates = formatTime(new Date())
      let res = service('GET', '/iot/rates/site/'+siteId).then(result => {
        console.log('shuju',result)
        if(result.data.code == 0){
          let list = result.data.list
          let data = result.data.list
          let current = ''
          let localIndex = 0
          data.map((item, index) => {
            item.begin_time =item.beginTime 
            item.end_time = item.endTime 
            item.coupon = item.service - (item.service * item.discount)
            // item.total = (item.electric + item.service -item.discount).toFixed(3)
            item.beginTime = new Date(dates+' '+item.beginTime).getTime()
            item.endTime = new Date(dates+' '+item.endTime).getTime()
            if(item.beginTime <= thisTime && item.endTime >= thisTime){
              current = index
              // some = item
            }
            return item
          })
          let groupsPrice = {}
          let currentGroups = {}
          if( result.data.groups){
            groupsPrice = result.data.groups.list
            groupsPrice.map((item, index) => {
              item.begin_time =item.beginTime 
              item.end_time = item.endTime 
              item.coupon = item.service - (item.service * item.discount)
              item.total = (item.electric + item.service -item.discount).toFixed(3)
              item.beginTime = new Date(dates+' '+item.beginTime).getTime()
              item.endTime = new Date(dates+' '+item.endTime).getTime()
              if(item.beginTime <= thisTime && item.endTime >= thisTime){
                localIndex = index
              }
              // return item
            })
          }
          let userTime = {}
          if(result.data.groups){
           userTime = result.data.groups.times
          }
          console.log('current',groupsPrice)
          that.setData({
            rateList:list,
            localRate: list,
            rate: list[current],
            currentIndex: current,
            userTime: userTime,
            groupPrice: groupsPrice,
            localIndex: localIndex
          })
        }
      })
    },
  }
})
